﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraBars.Helpers;
using TuongLaiDangCho.DTO;
using TuongLaiDangCho.DAO;
using TuongLaiDangCho.Enterface;
using DevExpress.XtraRichEdit;
using DevExpress.LookAndFeel;
using DevExpress.XtraCharts;
using System.IO;

namespace TuongLaiDangCho
{
    public partial class frmHome : DevExpress.XtraEditors.XtraForm
    {
        public static DateTime selectedDate;
        public static ucContain ucDiary;
        public static DateTime choiseDate;
        public ucNews ucnews;
        public static PanelControl pcstaticControl = new PanelControl();
        public ucExpenditure ucExpend;
        public ucTimeTable ucTimeTable;
        public ucNote ucNote;
        public ucUpdateChar ucChar;
        public static ucChar ucCharThoiGian;
        ucWriteDiary ucWrite;
        public static ucDanhNgon ucdanhngon;
        public static int b_flag;
        public static int index;
        public static int maxindex;
        public frmHome()
        {
            InitializeComponent();
            //tmrShowDanhNgon.Start();
            SkinHelper.InitSkinGallery(ribbonGalleryBarItem1, true);
            LoadSkin();
            dnCalendar.DateTime = DateTime.Today;
            choiseDate = DateTime.Today;
            b_flag=0;
            //MessageBox.Show("Test");
            //UserLookAndFeel.Default.SetSkinStyle("Liquid Sky");
            //ucDiary.lbContain.Click += new testEvent(lbTestClick_Click);
        }
        //public delegate void testEvent();
        #region "Hàm sử lí"
        private DateTime getDateCalendar()
        {
            if (dnCalendar.Selection.Count > 0)
            {
                return dnCalendar.Selection[0];
            }
            else
            {
                return dnCalendar.DateTime;
            }
        }

        private static void addUserControl_DiaryContain()
        {
            ucDiary = new ucContain();
            ucDiary.Location = new Point(0, 0);
            ucDiary.Size = new Size(669, 492);
            ucDiary.Parent = pcstaticControl;
            ucDiary.Name = "ucDiaryContain";
            ucDiary.Anchor = ((AnchorStyles)((((AnchorStyles.Top | AnchorStyles.Bottom) | AnchorStyles.Left) | AnchorStyles.Right)));
            pcstaticControl.Controls.Clear();
            pcstaticControl.Controls.Add(ucDiary);
            b_flag=0;
         }

        private void loadDiary()
        {
            NhatKiDTO _diary = new NhatKiDTO();
            _diary.Ngay = getDateCalendar();
            ucDiary.lbContain.Text = NhatKiDAO.LoadDiary(_diary);
            if (ucDiary.lbContain.Text == "")
            {
                ucDiary.lbContain.Text = "Không có nội dung để hiển thị";
            }
        }

        private void addWriteDiary()
        {
            ucWrite = new ucWriteDiary();
            ucWrite.Location =new Point(0, 0);
            ucWrite.Name = "ucWrite";
            ucWrite.Parent = pcstaticControl;
            pcstaticControl.Controls.Clear();
            pcstaticControl.Controls.Add(ucWrite);
        }
        public delegate void bntCloseClick();

        //bntCloseClick CloseClick = new bntCloseClick(changeUserControl);
        public static void changeUserControl()
        {
            pcstaticControl.Controls.Clear();
            addUserControl_DiaryContain();
            NhatKiDTO diary = new NhatKiDTO();
            diary.Ngay = choiseDate;
            ucDiary.lbContain.Text = NhatKiDAO.LoadDiary(diary);
            if (ucDiary.lbContain.Text == "")
            {
                ucDiary.lbContain.Text="Không có nội dung để hiển thị";
            }
        }        

        public void addPanelControl()
        {            
            pcstaticControl.Location = new Point(0,0);
            pcstaticControl.Anchor = ((AnchorStyles)((((AnchorStyles.Top | AnchorStyles.Bottom) | AnchorStyles.Left) | AnchorStyles.Right)));
            pcstaticControl.Size = new Size(669, 492);
            pcstaticControl.Name = "pcstaticControl";
            pcstaticControl.Parent = grcContent;
            grcContent.Controls.Clear();
            grcContent.Controls.Add(pcstaticControl);
        }
        public void AddUserControl_News()
        {
            pcstaticControl.Controls.Clear();
            ucnews = new ucNews();
            ucnews.Location = new Point(0, 0);
            ucnews.Name = "ucnews";
            ucnews.Parent = pcstaticControl;
            pcstaticControl.Controls.Add(ucnews);
        }

        public void AddUserControl_Expenditure()
        {
            pcstaticControl.Controls.Clear();
            ucExpend = new ucExpenditure();
            ucExpend.Location = new Point(0, 0);
            ucExpend.Name = "ucExpend";
            ucExpend.Parent = pcstaticControl;
            pcstaticControl.Controls.Add(ucExpend);
        }

        public void AddUserControl_TimeTable()
        {
            pcstaticControl.Controls.Clear();
            ucTimeTable = new ucTimeTable();
            ucTimeTable.Location = new Point(0, 0);
            ucTimeTable.Name = "ucTimeTable";
            ucTimeTable.Parent = pcstaticControl;
            pcstaticControl.Controls.Add(ucTimeTable);
        }

        public void AddUserControl_Note()
        {
            pcstaticControl.Controls.Clear();
            ucNote = new ucNote();
            ucNote.Location = new Point(0, 0);
            ucNote.Name = "ucNote";
            ucNote.Parent = pcstaticControl;
            pcstaticControl.Controls.Add(ucTimeTable);
        }

        public static void LoadChar()
        {
            if (ucCharThoiGian.chaThoiGian.Series.Count > 0)
            {
                ucCharThoiGian.chaThoiGian.BeginInit();
                Series series;
                series = ucCharThoiGian.chaThoiGian.Series[0];
                series.Points.Clear();
                DataTable dt = QuanLiThoiGianDAO.LoadChar();
                if (dt.Rows.Count < 3)
                {
                    if (dt.Rows[0]["HuuIch"].ToString() !="")
                    {
                        series.Points.Add(new SeriesPoint(1, dt.Rows[0]["HuuIch"]));
                    }
                    else
                    {
                        series.Points.Add(new SeriesPoint(1, 0));
                    }
                    if (dt.Rows[0]["KhongHuuIch"].ToString() != "")
                    {
                        series.Points.Add(new SeriesPoint(2, dt.Rows[0]["KhongHuuIch"]));
                    }
                    else
                    {
                        series.Points.Add(new SeriesPoint(2, 0));
                    }
                     if (dt.Rows[0]["KhongTen"].ToString() != "")
                    {
                        series.Points.Add(new SeriesPoint(3, dt.Rows[0]["KhongTen"]));
                    }
                    else
                    {
                        series.Points.Add(new SeriesPoint(3, 0));
                    }
                }
                series.Points[0].Argument = "Hữu Ích";
                series.Points[1].Argument = "Không Hữu Ích";
                series.Points[2].Argument = "Không Tên";
                ucCharThoiGian.chaThoiGian.EndInit();
            }
          
        }

        public void AddUserControl_UpdateChar()
        {
            pcstaticControl.Controls.Clear();
            ucChar = new ucUpdateChar();
            ucChar.Location = new Point(0, 0);
            ucChar.Parent = pcstaticControl;
            ucChar.Name = "ucChar";
            pcstaticControl.Controls.Add(ucChar);
        }
        public void AddUserControl_Char()
        {
            ucCharThoiGian = new ucChar();
            ucCharThoiGian.Location = new Point(671, 357);
            ucCharThoiGian.chaThoiGian.BackColor = (Color)this.BackColor;
            ucCharThoiGian.chaThoiGian.Legend.BackColor = (Color)this.BackColor;
            //ucCharThoiGian.Parent = pcChar;
            this.Controls.Add(ucCharThoiGian);
            //XtraMessageBox.Show("Test");
        }
        private static void addUserControl_DanhNgon()
        {
            ucdanhngon = new ucDanhNgon();
            ucdanhngon.Location = new Point(0, 0);
            ucdanhngon.Size = new Size(669, 492);
            ucdanhngon.Parent = pcstaticControl;
            ucdanhngon.Name = "ucDiaryContain";
            ucdanhngon.Anchor = ((AnchorStyles)((((AnchorStyles.Top | AnchorStyles.Bottom) | AnchorStyles.Left) | AnchorStyles.Right)));
            pcstaticControl.Controls.Clear();
            pcstaticControl.Controls.Add(ucdanhngon);
            // b_flag = ;
        }
        public void LoadSkin()
        {
            DataTable dt;
            dt = UserDAO.LoadSkin();
            if (dt.Rows.Count > 0)
            {
                UserLookAndFeel.Default.SetSkinStyle(dt.Rows[0][0].ToString());
                index = int.Parse(dt.Rows[0]["DanhNgonIndex"].ToString());
            }
            maxindex = DanhNgonDAO.LoadDanhNgon().Rows.Count;
        }

        #endregion
        
        #region "Sự kiện"
        //Form Load
        private void frmHome_Load(object sender, EventArgs e)
        {
            //frmLogin frmLongin = new frmLogin();
            //frmLongin.ShowDialog();
            addPanelControl();
            addUserControl_DiaryContain();
            loadDiary();
            //LoadChar();
            AddUserControl_Char();

        }        
        //click lên lịch
        private void dnCalendar_Click(object sender, EventArgs e)
        {
            choiseDate = getDateCalendar();
            //if (rbcMenu.SelectedPage.PageIndex == 0)
            //{
            //    loadDiary();
            //}
            switch (rbcMenu.SelectedPage.PageIndex)
            {
                case 0:
                    addUserControl_DiaryContain();
                    loadDiary();
                    //b_flag=0;
                    break;
                case 1:
                    //choiseDate = getDateCalendar();
                    AddUserControl_News();
                    break;
                case 2:
                    AddUserControl_Expenditure();
                    break;
                case 3:
                    AddUserControl_TimeTable();
                    break;
                //case 4:
                    //AddUserControl_Note();
                    //break;
                case 5:
                    AddUserControl_UpdateChar();
                    break;
                //case 6:
                //    rbcMenu.;
                default:
                    break;
            }
            
        }      
        //Khi font Style trên menu thay đổi
        private void beFontStyle_EditValueChanged(object sender, EventArgs e)
        {
            //ucDiary.lbContain.Font = new Font(beFontStyle.EditValue.ToString(), Convert.ToInt32(beFonSise.EditValue));
            //meDiary.Font = new Font(beFontStyle.EditValue.ToString(), Convert.ToInt32(beFonSise.EditValue));\
             switch(b_flag)
            {
                 case 1:
                    ucWrite.memoEdit.Font= new Font(beFontStyle.EditValue.ToString(), Convert.ToInt32(beFonSise.EditValue));
                     break;
                 case 0:
                    ucDiary.lbContain.Font = new Font(beFontStyle.EditValue.ToString(), Convert.ToInt32(beFonSise.EditValue));
                     break;
                 default:
                     break;
            }
        }
        //khi font size trên menu thay đổi
        private void beFonSise_EditValueChanged(object sender, EventArgs e)
        {
            switch(b_flag)
            {
                case 1:
                ucWrite.memoEdit.Font = new Font(beFontStyle.EditValue.ToString(), Convert.ToInt32(beFonSise.EditValue));
                    break;
                case 0:
                ucDiary.lbContain.Font = new Font(beFontStyle.EditValue.ToString(), Convert.ToInt32(beFonSise.EditValue));
                    break;
                default:
                    break;
            }
        }
        //khi font color thay đổi
        private void beiFontColor_EditValueChanged(object sender, EventArgs e)
        {
            //MessageBox.Show(beiFontColor.EditValue.ToString());
            switch(b_flag)
            {
                case 0:
                    ucDiary.lbContain.ForeColor = (Color)beiFontColor.EditValue;
                    break;
                case 1:
                    ucWrite.memoEdit.ForeColor=(Color)beiFontColor.EditValue;
                    break;
                default:
                    break;
            }
        }
        //khi nhấn nút Write trên menu
        private void bntWriteDiary_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            //choiseDate = getDateCalendar();
            addWriteDiary();
            b_flag=1;
        }
        //khi nhấn nút del trong tab Diary
        private void bntDelDiary_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (XtraMessageBox.Show("Are you sure?", "Delete Dialog", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
            {
                NhatKiDTO diary = new NhatKiDTO();
                diary.Ngay = getDateCalendar();
                try
                {
                    NhatKiDAO.DeleleDiary(diary);
                }
                catch { }
                loadDiary();
            }
        }
        //khi các tab trong Ribbon menu thay đổi
        private void rbcMenu_SelectedPageChanged(object sender, EventArgs e)
        {
            switch (rbcMenu.SelectedPage.PageIndex)
            {
                case 0:
                    b_flag = 0;
                    addUserControl_DiaryContain();
                    loadDiary();
                    break;
                case 1:
                    //choiseDate = getDateCalendar();
                    b_flag = 2;
                    AddUserControl_News();
                    break;
                case 2:
                    b_flag = 3;
                    AddUserControl_Expenditure();
                    break;
                case 3:
                    b_flag = 4;
                    AddUserControl_TimeTable();
                    break;
                case 4:
                    b_flag = 5;
                    AddUserControl_Note();
                    break;
                case 5:
                    b_flag = 6;
                    AddUserControl_UpdateChar();
                    break;
                case 6:
                    b_flag = 7;
                    addUserControl_DanhNgon();
                    break;
                default:
                    break;
            }
        }
        //hàm viết chung sự kiện cho tất cả các button delete
        private void bntExDelete_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {            
            //ucExpend.grv_Expenditure.SelectRow(
            switch (rbcMenu.SelectedPage.PageIndex)
            {
                case 1:
                    //choiseDate = getDateCalendar();
                    //XtraMessageBox.Show(ucnews.grv_News.GetRowCellValue(ucnews.grv_News.FocusedRowHandle, "ID").ToString());
                    if(ucnews.grv_News.SelectedRowsCount>0)
                    {
                        if (XtraMessageBox.Show("Are you sure?", "Quest", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
                        {
                            DieuMoiDTO news = new DieuMoiDTO();
                            news.Id = int.Parse((ucnews.grv_News.GetRowCellValue(ucnews.grv_News.FocusedRowHandle, "ID").ToString()));
                            DieuMoiDAO.DeleteNews(news);
                            news.NgayThang = choiseDate;
                            ucnews.grc_News.DataSource = DieuMoiDAO.LoadNews(news);
                        }
                    }
                    break;
                case 2:
                    if (ucExpend.grv_Expenditure.SelectedRowsCount > 0)
                    {
                        if (XtraMessageBox.Show("Are you sure?", "Quest", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
                        {
                           ChiTieuDTO expend = new ChiTieuDTO();
                           expend.Id = int.Parse(ucExpend.grv_Expenditure.GetRowCellValue(ucExpend.grv_Expenditure.FocusedRowHandle, "ID").ToString());
                           ChiTieuDAO.DeleteExpenditure(expend);
                           expend.NgayThang = choiseDate;
                           ucExpend.grc_Expenditure.DataSource = ChiTieuDAO.LoadExpenditure(expend);
                        }
                    }
                    break;
                case 3:
                    if (ucTimeTable.grv_TimeTable.SelectedRowsCount > 0)
                    {
                        if (XtraMessageBox.Show("Are you sure?", "Quest", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
                        {
                            LenLichDTO timetb = new LenLichDTO();
                            timetb.ID = int.Parse(ucTimeTable.grv_TimeTable.GetRowCellValue(ucTimeTable.grv_TimeTable.FocusedRowHandle, "ID").ToString());
                            LenLichDAO.DeleteTimeTable(timetb);
                            timetb.NgayThang = choiseDate;
                            ucTimeTable.grcTimeTable.DataSource = LenLichDAO.LoadTimeTable(timetb);
                        }
                    }
                    break;
                case 4:
                    if (ucNote.grv_Note.SelectedRowsCount > 0)
                    {
                        if (XtraMessageBox.Show("Are you sure?", "Quest", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
                        {
                            GhiChuDTO note = new GhiChuDTO();
                            note.ID = int.Parse(ucNote.grv_Note.GetRowCellValue(ucNote.grv_Note.FocusedRowHandle, "ID").ToString());
                            GhiChuDAO.DeleteNote(note);
                            ucNote.grcNote.DataSource = GhiChuDAO.LoadNote();
                        }
                    }
                    break;
                case 5:
                    if (ucChar.grv_UpdateChar.SelectedRowsCount > 0)
                    {                            
                        if (XtraMessageBox.Show("Are you sure?", "Quest", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
                        {
                            QuanLiThoiGianDTO timer = new QuanLiThoiGianDTO();
                            timer.ID = int.Parse(ucChar.grv_UpdateChar.GetRowCellValue(ucChar.grv_UpdateChar.FocusedRowHandle, "ID").ToString());
                            QuanLiThoiGianDAO.DeleteDataChar(timer);
                            timer.NgayThang = choiseDate;
                            ucChar.grcUpdateChar.DataSource = QuanLiThoiGianDAO.LoadDataChar(timer);
                            LoadChar();
                        }
                    }
                    break;
                case 6:
                    if (ucdanhngon.grv_DanhNgon.SelectedRowsCount > 0)
                    {
                        if (XtraMessageBox.Show("Are you sure?", "Quest", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
                        {
                            DanhNgonDTO danhngon = new DanhNgonDTO();
                            danhngon.Id = int.Parse(ucdanhngon.grv_DanhNgon.GetRowCellValue(ucdanhngon.grv_DanhNgon.FocusedRowHandle, "ID").ToString());
                            DanhNgonDAO.DeleteDanhNgon(danhngon);
                            danhngon.Date = choiseDate;
                            ucdanhngon.grcDanhNgon.DataSource = DanhNgonDAO.LoadDanhNgon();
                        }
                    }
                    break;
                default:
                    break;
            }
        }
        //khi nhấn nút backup
        private void bntBackup_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                FolderBrowserDialog fbdBackup = new FolderBrowserDialog();
                fbdBackup.ShowDialog();
                FileInfo file = new FileInfo(Application.StartupPath + @"\Database.mdb");
                file.CopyTo(fbdBackup.SelectedPath + @"\" + file.Name, true);
                XtraMessageBox.Show("Backup Success!", "Message", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            }
            catch
            {
                XtraMessageBox.Show("Error! Try Again", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }        
        //khi nhấn nút Restore
        private void bntRestore_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {    

            try
            {
                OpenFileDialog ofdRestore = new OpenFileDialog();
                //ofdRestore.
                ofdRestore.ShowDialog();
                FileInfo file = new FileInfo(ofdRestore.FileName);
                file.CopyTo(Application.StartupPath + @"\Database.mdb", true);
                XtraMessageBox.Show("Restore Success!"+"\n"+"Application will restart!", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
                Application.Restart();
            }
            catch
            {
                XtraMessageBox.Show("Error! Try Again", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
           
        }
        //sự kiện form đang đóng
        private void frmHome_FormClosing(object sender, FormClosingEventArgs e)
        {
            UserDTO user = new UserDTO();
            user.GiaoDien = UserLookAndFeel.Default.ActiveSkinName;
            user.DanhNgonIndex = index;
            UserDAO.UpdateSkin(user);

        }
        //click button Chang Password
        private void bntChangePW_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            frmChangePW _frmChangePW = new frmChangePW();
            //_frmChangePW.MdiChildren = true;
            _frmChangePW.ShowDialog();
        }

        //hàm sự kiện cho tất cả button copy to
        private void bntCopyToDate_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            //frmChuyenNgay _frmChuyenNgay = new frmChuyenNgay();
            //_frmChuyenNgay.ShowDialog();
            switch (rbcMenu.SelectedPage.PageIndex)
            {
                case 0:
                    NhatKiDTO diary = new NhatKiDTO();
                    diary.Ngay = getDateCalendar();
                    if (NhatKiDAO.LoadDiary(diary) == "")
                    {
                        XtraMessageBox.Show("Không có nội dung để chuyển", "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    else
                    {
                        frmChuyenNgay _frmChuyenNgay = new frmChuyenNgay();
                        _frmChuyenNgay.ShowDialog();
                        ucDiary.lbContain.Text = NhatKiDAO.LoadDiary(diary);
                        //b_flag = 0;
                    }
                    break;
                case 1:
                    DieuMoiDTO news = new DieuMoiDTO();
                    news.NgayThang = getDateCalendar();
                    if (DieuMoiDAO.LoadNews(news).Rows.Count <= 0)
                    {
                        XtraMessageBox.Show("Không có nội dung để chuyển", "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    else
                    {
                        frmChuyenNgay _frmChuyenNgay = new frmChuyenNgay();
                        _frmChuyenNgay.ShowDialog();
                        ucnews.grc_News.DataSource = DieuMoiDAO.LoadNews(news);
                        //b_flag = 2;
                    }
                    break;
                case 2:
                    ChiTieuDTO expend = new ChiTieuDTO();
                    expend.NgayThang = getDateCalendar();
                    if (ChiTieuDAO.LoadExpenditure(expend).Rows.Count <= 0)
                    {
                        XtraMessageBox.Show("Không có nội dung để chuyển", "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    else
                    {
                        frmChuyenNgay _frmChuyenNgay = new frmChuyenNgay();
                        _frmChuyenNgay.ShowDialog();
                        ucExpend.grc_Expenditure.DataSource = ChiTieuDAO.LoadExpenditure(expend);
                        //b_flag=
                    }
                    break;
                case 3:
                    LenLichDTO timetable = new LenLichDTO();
                    timetable.NgayThang = getDateCalendar();
                    if (LenLichDAO.LoadTimeTable(timetable).Rows.Count <= 0)
                    {
                        XtraMessageBox.Show("Không có nội dung để chuyển", "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    else
                    {
                        frmChuyenNgay _frmChuyenNgay = new frmChuyenNgay();
                        _frmChuyenNgay.ShowDialog();
                        ucTimeTable.grcTimeTable.DataSource = LenLichDAO.LoadTimeTable(timetable);
                    }
                    break;
                //case 4:
                //    //GhiChuDTO note = new GhiChuDTO();
                //    if (GhiChuDAO.LoadNote().Rows.Count <= 0)
                //    {
                //        XtraMessageBox.Show("Không có nội dung để chuyển", "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
                //    }
                //    else
                //    {
                //        frmChuyenNgay _frmChuyenNgay = new frmChuyenNgay();
                //        _frmChuyenNgay.ShowDialog();
                //    }
                //    break;
                case 5:
                    QuanLiThoiGianDTO dataChar = new QuanLiThoiGianDTO();
                    dataChar.NgayThang = getDateCalendar();
                    if (QuanLiThoiGianDAO.LoadDataChar(dataChar).Rows.Count <= 0)
                    {
                        XtraMessageBox.Show("Không có nội dung để chuyển", "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    else
                    {
                        frmChuyenNgay _frmChuyenNgay = new frmChuyenNgay();
                        _frmChuyenNgay.ShowDialog();
                        ucChar.grcUpdateChar.DataSource = QuanLiThoiGianDAO.LoadDataChar(dataChar);
                    }
                    break;
                default:
                    break;
            }
        }

        //sự kiện form Home thay đổi màu nền.
        private void frmHome_BackColorChanged(object sender, EventArgs e)
        {
            ucCharThoiGian.chaThoiGian.BackColor = (Color)this.BackColor;
            ucCharThoiGian.chaThoiGian.Legend.BackColor = (Color)this.BackColor;
            //XtraMessageBox.Show(backColor.ToString());
        }
        //sự kiện double click lên icon system tray
        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                this.Show();
                this.WindowState = FormWindowState.Normal;
                this.Activate();
                notifyIcon1.Visible = false;
            }
        }
        //sự kiện form home thay đổi kích thước
        private void frmHome_Resize(object sender, EventArgs e)
        {
            if (FormWindowState.Minimized == this.WindowState)
            {
                notifyIcon1.Visible = true;
                this.Hide();
                notifyIcon1.BalloonTipText = "Chương trình đã được thu nhỏ xuống khay hệ thống, bạn có thể khôi phục chương trình từ đây";
                notifyIcon1.ShowBalloonTip(5000);
                tmrShowDanhNgon.Start();
            }
            else
            {
                if (FormWindowState.Normal == this.WindowState)
                {
                    notifyIcon1.Visible = false;
                    tmrShowDanhNgon.Stop();
                }
            }
        }
        //sự kiện nhất exit trên context menu ở system tray
        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        //sự kiện nhấn vào nút show trên context menu system tray
        private void showToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                this.Show();
                this.WindowState = FormWindowState.Normal;
                this.Activate();
                notifyIcon1.Visible = false;
            }
        }
        //sự kiện click lên balloon tip trên system tray
        private void notifyIcon1_BalloonTipClicked(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                this.Show();
                this.WindowState = FormWindowState.Normal;
                this.Activate();
                notifyIcon1.Visible = false;
            }
        }

        private void tmrShowDanhNgon_Tick(object sender, EventArgs e)
        {
            if (index >= maxindex)
            {
                index = 0;
            }
            DataTable dt = DanhNgonDAO.LoadDanhNgon();
            string s_DanhNgon = dt.Rows[index]["NoiDung"].ToString();
            string s_TacGia = dt.Rows[index]["TacGia"].ToString();
            
            notifyIcon1.ShowBalloonTip(s_DanhNgon.Length*1000, "Danh Ngon", s_DanhNgon+"\n"+s_TacGia + "\nLength: " +s_DanhNgon.Length.ToString(), ToolTipIcon.Info);
            index++;

        }
        //sự  kiện click button Delete trong Danh Ngôn
        #endregion          
       
    }
}